/*
 * Event.h
 *
 *  Created on: May 20, 2013
 *      Author: root
 */

#ifndef EVENT_H_
#define EVENT_H_

#include <pthread.h>
#include <semaphore.h>
#include <errno.h>

class CEvent{
private:
	sem_t  	_qsem;

public:
	CEvent(){
		int nRet = sem_init(&_qsem,0,0);
		if(nRet != 0){
			printf("failed to create event,sem_init failed:%d\n",nRet);
		}
	}

	virtual ~CEvent(){
		sem_destroy(&_qsem);
	}

	void wait(){
		//todo check return value;
		struct timespec ts;
		clock_gettime(CLOCK_REALTIME, &ts);
		ts.tv_sec += 160;
		int r = sem_timedwait(&_qsem,&ts);
		if(r == -1 && errno == ETIMEDOUT){
			throw "event wait failed.";
		}
	}

	void fire(){
		//todo check return value;
		sem_post(&_qsem);
	}
};


#endif /* EVENT_H_ */
